#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under GPL v3
# Copyright 2008, Uğur Çetin <ugur.jnmbk@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtSql

from ui_mainwindow import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)

        settings = QtCore.QSettings()
        self.database = QtSql.QSqlDatabase.addDatabase("QSQLITE")
        self.database.setDatabaseName(settings.value("database/fileLocation", QtCore.QVariant("data/iett.db")).toString())
        self.database.open()
        self.model = QtSql.QSqlQueryModel()

        query = QtSql.QSqlQuery("select name from busses")
        query.exec_()
        while query.next():
            self.comboBox.addItem(query.value(0).toString())
        self.timeEdit.setTime(QtCore.QTime.currentTime().addSecs(-3600))
        self.timeEdit_2.setTime(QtCore.QTime.currentTime().addSecs(3600))

        QtCore.QObject.connect(self.timeEdit, QtCore.SIGNAL("timeChanged(const QTime &)"), self.query)
        QtCore.QObject.connect(self.timeEdit_2, QtCore.SIGNAL("timeChanged(const QTime &)"), self.query)
        QtCore.QObject.connect(self.comboBox, QtCore.SIGNAL("currentIndexChanged(int)"), self.query)
        QtCore.QObject.connect(self.radioButton, QtCore.SIGNAL("toggled(bool)"), self.query)
        QtCore.QObject.connect(self.radioButton_2, QtCore.SIGNAL("toggled(bool)"), self.query)

        #load settings
        self.comboBox.setCurrentIndex(settings.value("bus", QtCore.QVariant(0)).toInt()[0])
        if not settings.value("stop", QtCore.QVariant(True)).toBool():
            self.radioButton_2.setChecked(True)
        self.query()

    def query(self):
        startTime = self.timeEdit.time().toString("HHmm")
        endTime = self.timeEdit_2.time().toString("HHmm")
        bus = self.comboBox.currentText()

        #first, get the start and end points for the bus
        query = QtSql.QSqlQuery()
        query.prepare("SELECT stops.name FROM stops, busstops, busses WHERE busses.id=busstops.bus_id AND stops.id=busstops.stop_id AND busses.name=:bus ORDER BY sequence ASC")
        query.bindValue(":bus", QtCore.QVariant(bus))
        query.exec_()
        query.first()
        firstStop = query.value(0)
        query.last()
        lastStop = query.value(0)
        self.radioButton.setText(firstStop.toString())
        self.radioButton_2.setText(lastStop.toString())
        if self.radioButton_2.isChecked():
            firstStop = lastStop

        #get the times
        query.prepare("select times.time from times, busses, stops, daytypes where busses.id=times.bus_id and stops.id=times.stop_id and daytypes.id=times.daytype_id and stops.name=:stop and busses.name=:bus and daytypes.name=:dayType and times.time>:startTime and times.time<:endTime")
        query.bindValue(":stop", firstStop)
        query.bindValue(":bus", QtCore.QVariant(bus))
        query.bindValue(":dayType", QtCore.QVariant("Weekdays"))
        query.bindValue(":startTime", QtCore.QVariant(startTime))
        query.bindValue(":endTime", QtCore.QVariant(endTime))
        query.exec_()

        self.model.setQuery(query)
        self.model.setHeaderData(0, QtCore.Qt.Horizontal, firstStop)
        self.tableView.setModel(self.model)

    def on_pushButton_clicked(self):
        settings = QtCore.QSettings()
        settings.setValue("bus", QtCore.QVariant(self.comboBox.currentIndex()))
        settings.setValue("stop", QtCore.QVariant(self.radioButton.isChecked()))
